from rest_framework import serializers
from django_celery_beat.models import PeriodicTask, CrontabSchedule, IntervalSchedule, ClockedSchedule




class IntervalSerializer(serializers.ModelSerializer):
    class Meta:
        model = IntervalSchedule
        fields = '__all__'


class CrontabSerializer(serializers.ModelSerializer):
    class Meta:
        model = CrontabSchedule
        exclude = ['timezone']


class ClockedSerializer(serializers.ModelSerializer):
    class Meta:
        model = ClockedSchedule
        fields = '__all__'


class TaskSerializer(serializers.ModelSerializer):
    schedule = serializers.SerializerMethodField()
    time_type = serializers.SerializerMethodField()

    class Meta:
        model = PeriodicTask
        fields = '__all__'

    def get_schedule(self, obj):
        if obj.interval:
            return obj.interval.__str__()
        elif obj.crontab:
            return obj.crontab.__str__()
        elif obj.clocked:
            return obj.clocked.__str__()
        return ''

    def get_time_type(self, obj):
        if obj.interval:
            return 'interval'
        elif obj.crontab:
            return 'crontab'
        elif obj.clocked:
            return 'clocked'
        return 'interval'
